<!--
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-12-30 11:22:12
 * @LastEditors: 朱安武 549410045@qq.com
 * @LastEditTime: 2023-01-06 16:06:28
 * @FilePath: \html学习\vue-test\elementUi\src\components\LoginMain.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
        <el-card shadow="always" class="outbox">
            <el-form-item prop="pass">
                <h1 class="title">嗨购平台管理</h1>
                <el-input v-model="ruleForm.pass" type="text" autocomplete="off">
                    <template #prefix>
                        <el-icon>
                            <User />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
                <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off">
                    <template #prefix>
                        <el-icon>
                            <Lock />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                <el-button @click="()=>$emit('changeShowLogin')">注册</el-button>
            </el-form-item>
        </el-card>
    </el-form>
</template>
  
<script lang="ts" setup>
import { reactive, ref,toRefs } from 'vue'
import type { FormInstance } from 'element-plus'
//导入router
import { router } from '../router'
//导入axios 方法
import {login} from '../services/index'
//导入pinia 方法
import { useAdminStore }from '../stores/admin-store'

//setup中直接调用 点语法调用里面的函数 
const setAdmin = useAdminStore()


const ruleFormRef = ref<FormInstance>()

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('未填写用户名'))
    } else {
        
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('未填写密码'))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    checkPass: '',
})

const rules = reactive({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            // ruleForm.pass === 'admin' && ruleForm.checkPass === 'admin' && router.push('/MainPage') || alert('账号密码为admin')
            login({
                 adminname: ruleForm.pass,
                 password: ruleForm.checkPass
            }).then((result) => { 
            if (result.data.code === '200') {
                router.push('/mainPage/mainHome')
                //传递token ,adminname
                localStorage.setItem('token',result.data.data.token)
                localStorage.setItem('adminname',result.data.data.adminname)
                //设置adminname
                setAdmin.setAdminStore(result.data.data.adminname)
            } 
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    
}

</script>


<style scoped>
.demo-ruleForm {
    width: 340px;
    margin: 200px auto auto;
    padding: 50px;
}
.title {
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 20px;
}
.outbox{
    border-radius: 30px;
}
</style>
  

